package com.bluesky.drcp.business.service;

import java.util.List;

import com.bluesky.drcp.business.Plugin;
import com.bluesky.drcp.persistence.dao.PluginDao;

public class PluginService {
	private PluginDao pluginDao;
	

	public PluginDao getPluginDao() {
		return pluginDao;
	}

	public void setPluginDao(PluginDao pluginDao) {
		this.pluginDao = pluginDao;
	}

	public String getIdByName(String name) {
		Plugin p = getByName(name);

		if (p == null)
			return null;
		else
			return p.getId();
	}

	public Plugin get(String id) {
		return pluginDao.get(id);
	}

	public List getAll(){
		return pluginDao.getAll();
	}
	
	public String getNameById(String id) {
		Plugin o = pluginDao.get(id);
		return o.getName();

	}

	public Plugin getByName(String name) {

		return pluginDao.getByName(name);

	}

	public String getVersionByName(String name) {
		Plugin o = getByName(name);
		return o.getVersion();
	}

	public byte[] getDataByName(String name) {
		Plugin o = getByName(name);
		return o.getData();
	}

	public void saveOrUpdateByName(Plugin o) {

		Plugin o2 = pluginDao.getByName(o.getName());
		if (o2 == null) {
			if(o.getId()==null)
				o.setId(o.getName());
			
			pluginDao.insert(o);
		} else {
			o2.setName(o.getName());
			o2.setVersion(o.getVersion());
			o2.setDesc(o.getDesc());
			o2.setData(o.getData());
			
			pluginDao.saveOrUpdate(o2);
		}

	}
	
}
